<?php

namespace App\Http\Controllers\Clbum;

use App\Criteria\Clbum\Clbum\ClbumBaseQueryCriteria;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CurlRecordMethod;
use App\Http\Requests\Clbum\ClbumBaseRequest;
use App\Libs\Tools\SystemCodeConstant;
use App\Repositories\Clbum\ClbumExtRepository;
use App\Repositories\Clbum\ClbumRepository;
use App\Repositories\Clbum\GradeRepository;
use App\Repositories\Student\StudentRepository;
use App\Repositories\System\SystemCodeRepository;
use App\Repositories\System\UserRepository;
use App\Validators\Clbum\ClbumValidator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Prettus\Validator\Contracts\ValidatorInterface;
use Prettus\Validator\Exceptions\ValidatorException;


class ClbumsController extends Controller
{

    use CurlRecordMethod;
    protected $index_page='clbum.clbum.clbumMain';
    protected $add_page='clbum.clbum.clbumAdd';
    protected $edit_page='clbum.clbum.clbumEdit';
    protected $student_page='clbum.clbum.clbumStudentMain';
    protected $add_student_page='clbum.clbum.clbumStudentAdd';

    protected $repository;
    protected $validator;
    protected $request;
    protected $userRepository;
    protected $clbumExtRepository;
    protected $gradeRepository;
    protected $systemCodeRepository;
    protected $studentRepository;
    public function __construct(ClbumRepository $repository,
                                 ClbumValidator $validator,
                                 ClbumBaseRequest $request,
                                 UserRepository $userRepository,
                                 ClbumExtRepository $clbumExtRepository,
                                 GradeRepository $gradeRepository,
                                 SystemCodeRepository $systemCodeRepository,
                                 StudentRepository $studentRepository)
    {
        $this->repository = $repository;
        $this->validator  = $validator;
        $this->request =  $request;
        $this->userRepository = $userRepository;
        $this->clbumExtRepository= $clbumExtRepository;
        $this->gradeRepository =$gradeRepository;
        $this->systemCodeRepository=$systemCodeRepository;
        $this->studentRepository=$studentRepository;
    }

    public function getBaseQueryCriteria(){
        return ClbumBaseQueryCriteria::class;
    }
    public function get_add_data()
    {
        $users=$this->userRepository->all();
        $grades=$this->gradeRepository->all();
        $CLBUM_EXT_COURSE_LEADERS=$this->systemCodeRepository->findByField('type',SystemCodeConstant::CLBUM_EXT_COURSE_LEADER_TYPE);
        $CLBUM_EXT_COURSE_TEACHERS=$this->systemCodeRepository->findByField('type',SystemCodeConstant::CLBUM_EXT_COURSE_TEACHER_TYPE);
        $CLBUM_EXT_STUDENT_LEADERS=$this->systemCodeRepository->findByField('type',SystemCodeConstant::CLBUM_EXT_STUDENT_LEADER_TYPE);
        return view($this->add_page,compact('users','grades','CLBUM_EXT_COURSE_LEADERS','CLBUM_EXT_COURSE_TEACHERS','CLBUM_EXT_STUDENT_LEADERS'));
    }

    public function get_edit_data()
    {
        $id=$this->request->get('id');
        $editEntity = $this->repository->skipPresenter()->with(['clbumExts'])->find($id);
        $grades=$this->gradeRepository->all();
        $users=$this->userRepository->all();
        $CLBUM_EXT_COURSE_LEADERS=$this->systemCodeRepository->findByField('type',SystemCodeConstant::CLBUM_EXT_COURSE_LEADER_TYPE);
        $CLBUM_EXT_COURSE_TEACHERS=$this->systemCodeRepository->findByField('type',SystemCodeConstant::CLBUM_EXT_COURSE_TEACHER_TYPE);
        $CLBUM_EXT_STUDENT_LEADERS=$this->systemCodeRepository->findByField('type',SystemCodeConstant::CLBUM_EXT_STUDENT_LEADER_TYPE);
        return view($this->edit_page,compact('editEntity','users','grades','CLBUM_EXT_COURSE_LEADERS','CLBUM_EXT_COURSE_TEACHERS','CLBUM_EXT_STUDENT_LEADERS'));
    }

    public function post_add_data()
    {
        try {
            $this->validator->with($this->request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
            DB::transaction(function () {
                $all=$this->wrap_user_info($this->request->all(),true);
                $clbum=$this->repository->skipPresenter()->create($all);

                $CLBUM_EXT_COURSE_LEADERS=$all['CLBUM_EXT_COURSE_LEADERS'];
                $CLBUM_EXT_COURSE_TEACHERS=$all['CLBUM_EXT_COURSE_TEACHERS'];
                $CLBUM_EXT_STUDENT_LEADERS=$all['CLBUM_EXT_STUDENT_LEADERS'];

                $COURSE_LEADER=$this->create_clbum_exts($clbum->id,SystemCodeConstant::CLBUM_EXT_COURSE_LEADER_TYPE,$CLBUM_EXT_COURSE_LEADERS);
                $COURSE_TEACHERS=$this->create_clbum_exts($clbum->id,SystemCodeConstant::CLBUM_EXT_COURSE_TEACHER_TYPE,$CLBUM_EXT_COURSE_TEACHERS);
                $STUDENT_LEADERS=$this->create_clbum_exts($clbum->id,SystemCodeConstant::CLBUM_EXT_STUDENT_LEADER_TYPE,$CLBUM_EXT_STUDENT_LEADERS);

                $this->save_clbum_exts($COURSE_LEADER);
                $this->save_clbum_exts($COURSE_TEACHERS);
                $this->save_clbum_exts($STUDENT_LEADERS);
            });
            return $this->return_json_success();
        } catch (ValidatorException $e) {
            return $this->return_json_error($e);
        }
    }

    protected function post_edit_data(){
        try {
            $this->validator->with($this->request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
            DB::transaction(function () {
                $id=$this->request->get('id');
                $all=$this->wrap_user_info($this->request->all());
                $clbum=$this->repository->skipPresenter()->update($all, $id);
                $clbum->clbumExts()->delete();

                $CLBUM_EXT_COURSE_LEADERS=$all['CLBUM_EXT_COURSE_LEADERS'];
                $CLBUM_EXT_COURSE_TEACHERS=$all['CLBUM_EXT_COURSE_TEACHERS'];
                $CLBUM_EXT_STUDENT_LEADERS=$all['CLBUM_EXT_STUDENT_LEADERS'];

                $COURSE_LEADER=$this->create_clbum_exts($clbum->id,SystemCodeConstant::CLBUM_EXT_COURSE_LEADER_TYPE,$CLBUM_EXT_COURSE_LEADERS);
                $COURSE_TEACHERS=$this->create_clbum_exts($clbum->id,SystemCodeConstant::CLBUM_EXT_COURSE_TEACHER_TYPE,$CLBUM_EXT_COURSE_TEACHERS);
                $STUDENT_LEADERS=$this->create_clbum_exts($clbum->id,SystemCodeConstant::CLBUM_EXT_STUDENT_LEADER_TYPE,$CLBUM_EXT_STUDENT_LEADERS);

                $this->save_clbum_exts($COURSE_LEADER);
                $this->save_clbum_exts($COURSE_TEACHERS);
                $this->save_clbum_exts($STUDENT_LEADERS);
            });

            return $this->return_json_success();
        } catch (ValidatorException $e) {
            return $this->return_json_error($e);
        }
    }

    public function student_index(){
        $clbum_id=Input::get('clbum_id');
        return view($this->student_page,compact('clbum_id'));

    }

    private function create_clbum_exts($clbum_id,$type,$values){
        $clbum_exts=[];
        foreach ($values as $key=>$value){
            array_push($clbum_exts,[
                'clbum_id'=>$clbum_id,
                'system_code_type'=>$type,
                'system_code_value'=>$value,
                'created_user'=>Auth::user()['name'],
                'updated_user'=>Auth::user()['name'],
            ]);
        }
        return $clbum_exts;
    }

    private function save_clbum_exts($clbum_exts){
        foreach ($clbum_exts as $key=>$clbum_ext){
            $this->clbumExtRepository->create($clbum_ext);
        }
    }

    public function get_add_student(){
        $clbum_id=Input::get('clbum_id');
        $students=$this->studentRepository->skipPresenter()->findByField('clbum_id','0');
        return view($this->add_student_page,compact('students','clbum_id'));
    }

    public function post_add_student(){
        $clbum_id=Input::get('clbum_id');
        $students=Input::get('students');
        if(!$students){
            return $this->return_json_error_message('班级成员不能为空');
        }
        DB::table('student')->whereIn('id',$students)->update([
            'clbum_id'=>$clbum_id,
            'updated_user'=>Auth::user()['name'],
            'updated_at'=>date('Y-m-d H:i:s')
        ]);
        return $this->return_json_success();
    }
}
